package ueg.org.trabalho.view;

import java.awt.Dimension;
import javax.swing.JOptionPane;
import ueg.org.trabalho.dao.DaoEmpresa;
import ueg.org.trabalho.interfaces.IDao;
import ueg.org.trabalho.model.Empresa;
import ueg.org.trabalho.util.Util;

public class GUICadastroEmpresa extends javax.swing.JInternalFrame {

    public GUICadastroEmpresa() {
        initComponents();


    }

    public void setPosicao() {
        Dimension d = this.getDesktopPane().getSize();
        this.setLocation((d.width - this.getSize().width) / 2,
                (d.height - this.getSize().height) / 2);
    }

    public Empresa setEmpresa() {
        Empresa empresa = new Empresa();
        empresa.setNome(tfNome.getText().trim());
        empresa.setCnpj(ftfCNPJ.getText().trim());
        return empresa;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CadastroDepartamento = new javax.swing.JPanel();
        lbNome = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        lbCNPJ = new javax.swing.JLabel();
        ftfCNPJ = new javax.swing.JFormattedTextField();
        bSalvar = new javax.swing.JButton();
        bCancelar = new javax.swing.JButton();
        bLimparCampos = new javax.swing.JButton();

        setClosable(true);

        CadastroDepartamento.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cadastro Empresa", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));

        lbNome.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbNome.setText("Nome");

        lbCNPJ.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbCNPJ.setText("CNPJ");

        try {
            ftfCNPJ.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftfCNPJ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ftfCNPJActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout CadastroDepartamentoLayout = new javax.swing.GroupLayout(CadastroDepartamento);
        CadastroDepartamento.setLayout(CadastroDepartamentoLayout);
        CadastroDepartamentoLayout.setHorizontalGroup(
            CadastroDepartamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CadastroDepartamentoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(CadastroDepartamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbNome)
                    .addComponent(lbCNPJ))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(CadastroDepartamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftfCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        CadastroDepartamentoLayout.setVerticalGroup(
            CadastroDepartamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CadastroDepartamentoLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(CadastroDepartamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbNome)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(CadastroDepartamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ftfCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCNPJ))
                .addContainerGap(77, Short.MAX_VALUE))
        );

        bSalvar.setText("Salvar");
        bSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSalvarActionPerformed(evt);
            }
        });

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        bLimparCampos.setText("Limpar Campos");
        bLimparCampos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLimparCamposActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(CadastroDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(bSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(bCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bLimparCampos)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bCancelar, bSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(CadastroDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 74, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancelar)
                    .addComponent(bSalvar)
                    .addComponent(bLimparCampos))
                .addGap(26, 26, 26))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {bCancelar, bSalvar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bLimparCamposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLimparCamposActionPerformed
        try {
            Util.LimpaTela(CadastroDepartamento);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Não foi possivel limpar os campos!");
        }
    }//GEN-LAST:event_bLimparCamposActionPerformed

    private void bSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSalvarActionPerformed
        Empresa empresa = new Empresa();
        empresa = setEmpresa();
        IDao<Empresa> iDao = new DaoEmpresa();
        try {
            iDao.salva(empresa);
            JOptionPane.showMessageDialog(null, "Salvo com sucesso!");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Não foi possivel salvar a Empresa!motivo: " + e);
        }
    }//GEN-LAST:event_bSalvarActionPerformed

    private void ftfCNPJActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ftfCNPJActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ftfCNPJActionPerformed

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bCancelarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel CadastroDepartamento;
    private javax.swing.JButton bCancelar;
    private javax.swing.JButton bLimparCampos;
    private javax.swing.JButton bSalvar;
    private javax.swing.JFormattedTextField ftfCNPJ;
    private javax.swing.JLabel lbCNPJ;
    private javax.swing.JLabel lbNome;
    private javax.swing.JTextField tfNome;
    // End of variables declaration//GEN-END:variables
}
